package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HDriverLicenseMapper;
import com.flea.hclv.model.dao.HDriverMapper;
import com.flea.hclv.model.dao.HResourceRelationMapper;
import com.flea.hclv.model.entity.HDriver;
import com.flea.hclv.model.entity.HDriverLicense;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.service.HDriverService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.utils.FleaStringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: project
 * @description: 司机
 * @author: shiliqiang
 * @create: 2018-09-26 20:49
 * @version: 1.0
 */
@Service
public class HDriverServiceImpl implements HDriverService {

    @Autowired
    private HDriverMapper hDriverMapper;
    @Autowired
    private HResourceRelationMapper hResourceRelationMapper;
    @Autowired
    private HDriverLicenseMapper hDriverLicenseMapper;

    @Override
    public ServerResponse<String> saveOrUpdate(String userId, OrgDept currentUserDept, HDriver driver, List<HResourceRelation> hResourceRelations, HDriverLicense hDriverLicense) {
        if (driver==null){
            return ServerResponse.createByErrorMessage("司机信息不能为空");
        }
        if (hDriverLicense==null){
            return ServerResponse.createByErrorMessage("驾驶证信息不能为空");
        }
        if (hResourceRelations.size()==0){
            return ServerResponse.createByErrorMessage("驾驶证照片不能为空");
        }
        String id = driver.getId();
        int result;
        if (id == null) {
            id = FleaStringUtils.getGenerateUUID();
            driver.setId(id);
            driver.setDeptId(currentUserDept==null? HConstant.HCLV_ADMIN_ADMIN:currentUserDept.getDeptUuid());
            driver.setCreateUser(userId);
            driver.setModifyUser(userId);
            result = hDriverMapper.insertSelective(driver);
            if (result<0){
                return ServerResponse.createByErrorMessage("司机添加失败");
            }
            String licenseId = FleaStringUtils.getGenerateUUID();
            String code = FleaStringUtils.getGenerateUUID();
            hDriverLicense.setCode(code);
            hDriverLicense.setId(licenseId);
            hDriverLicense.setDriverId(id);
            hDriverLicense.setCreateUser(userId);
            hDriverLicense.setModifyUser(userId);
            result = hDriverLicenseMapper.insertSelective(hDriverLicense);
            if (result<0){
                return ServerResponse.createByErrorMessage("驾驶证添加失败");
            }
            return insertResourceRelations(hResourceRelations,licenseId,userId);
        }else {
            driver.setModifyUser(userId);
            result = hDriverMapper.updateByPrimaryKeySelective(driver);
            if (result<0){
                return ServerResponse.createByErrorMessage("司机基本信息更新失败");
            }
            hDriverLicense.setModifyUser(userId);
            result = hDriverLicenseMapper.updateByPrimaryKeySelective(hDriverLicense);
            if (result<0){
                return ServerResponse.createByErrorMessage("行驶证基本信息更新失败");
            }
            return updateResourceRelations(hResourceRelations,id,userId);
        }
    }
    private ServerResponse<String> updateResourceRelations(List<HResourceRelation> list,String uuid,String userId){
        int row = hResourceRelationMapper.deleteByUuid(uuid);
        if (row<0){
            return ServerResponse.createByErrorMessage("行驶证图片更新失败");
        }
        return insertResourceRelations(list,uuid,userId);

    }
    private ServerResponse<String> insertResourceRelations(List<HResourceRelation> list,String uuid,String userId){
        if (list.size()==0){
            return ServerResponse.createByErrorMessage("行驶证照片不能为空");
        }
        List<HResourceRelation> hResourceRelations = new ArrayList<>();
        int row;
        for (HResourceRelation hResourceRelation:list){
            String id = FleaStringUtils.getGenerateUUID();
            hResourceRelation.setId(id);
            hResourceRelation.setCreateUser(userId);
            hResourceRelation.setModifyUser(userId);
            hResourceRelation.setUuid(uuid);
            hResourceRelations.add(hResourceRelation);
        }
        row=hResourceRelationMapper.insertList(hResourceRelations);
        if (row<0){
            return ServerResponse.createByErrorMessage("行驶证图片添加失败");
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<FleaPage<HDriver>> selectByDeptId(OrgDept currentUserDept, int curPage, int pageSize, String name, String type) {
        if (currentUserDept==null){
            return ServerResponse.createByErrorMessage("没有找到该用户的部门信息");
        }
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HDriver> list = hDriverMapper.selectByDeptId(rowBounds,currentUserDept.getDeptUuid(),name,type);
        FleaPage<HDriver> hDriverFleaPage = new FleaPage<>(list);
        return ServerResponse.createBySuccess(hDriverFleaPage);
    }

    @Override
    public ServerResponse<FleaPage<HDriver>> selectAll(int curPage, int pageSize, String name, String type) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HDriver> list = hDriverMapper.selectByDeptId(rowBounds,null,name,type);
        return ServerResponse.createBySuccess(new FleaPage<>(list));
    }

    @Override
    public ServerResponse<JSONObject> selectLicenseAndImage(String driverId) {
        if (driverId==null){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        HDriverLicense hDriverLicense = hDriverLicenseMapper.selectByDriverId(driverId);
        if (hDriverLicense==null){
            return ServerResponse.createByErrorMessage("没有找到该驾驶证");
        }
        List<HResourceRelation> hResourceRelation = hResourceRelationMapper.selectByUuid(hDriverLicense.getId());
        if (hResourceRelation.size()==0){
            return ServerResponse.createByErrorMessage("没有找到该驾驶证图片");
        }
        //拼json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hDriverLicense",hDriverLicense);
        jsonObject.put("hResourceRelation",hResourceRelation);
        return ServerResponse.createBySuccess(jsonObject);
    }

    @Override
    public ServerResponse<String> deleteByIds(String userId, List<String> idList) {
        if (idList.size()==0){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        int result = hDriverMapper.deleteByIds(idList,userId);
        if (result<=0){
            return ServerResponse.createByErrorMessage("操作失败");
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }
}
